package com.lyf.scm.admin.api.controller;


import com.github.pagehelper.PageInfo;
import com.lyf.scm.admin.api.dto.OperationLogRecordDTO;
import com.lyf.scm.admin.domain.service.OperationLogRecordService;
import com.lyf.scm.admin.domain.util.DownLoadUtil;
import com.rome.arch.core.clientobject.Response;
import com.rome.arch.util.controller.RomeController;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * description:
 * 操作日志记录
 *
 * @author: huangtao
 * @data: 2020/10/5
 */
@Api(tags = "操作日志记录")
@RequestMapping("/api/v1/operationLogRecord")
@RomeController
public class OperationLogRecordController {

    @Resource
    private OperationLogRecordService operationLogRecordService;

    @PostMapping("/getOperationLogs")
    @ApiOperation("查询操作日志表")
    public Response<PageInfo<OperationLogRecordDTO>> getOperationLogs(@RequestBody OperationLogRecordDTO operationLogRecord) {
        return Response.builderSuccess(operationLogRecordService.getOperationLogs(operationLogRecord));
    }

    @GetMapping("/downLoad")
    @ApiOperation("下载")
    public void downLoad(@RequestParam("ext") String ext, HttpServletResponse httpServletResponse) throws Exception {
        DownLoadUtil.download(httpServletResponse, new File(ext));
    }

    @PostMapping("/delete")
    @ApiOperation("删除操作标识、数据源对应数据")
    public Response delete(@RequestParam String opSign, @RequestParam Integer type) {
        operationLogRecordService.delete(opSign, type);
        return Response.builderSuccess(null);
    }

    @PostMapping("/bigData/Initiating")
    @ApiOperation(value = "发起大数据定时任务，将状态调整为进行中")
    public Response bigDataInitiating() {
        operationLogRecordService.bigDataInitiating();
        return Response.builderSuccess(null);
    }

    @PostMapping("/opLog/Initiating")
    @ApiOperation(value = "发起删除任务（成功的操作数据），将状态调整为进行中")
    public Response opLogInitiating(@RequestBody List<Long> ids) {
        operationLogRecordService.opLogInitiating(ids);
        return Response.builderSuccess(null);
    }

    @DeleteMapping
    @ApiOperation(value = "删除指定标识数据")
    public Response rmByOpSign(@RequestBody List<OperationLogRecordDTO> operationLogRecordDTOs) {
        operationLogRecordService.rmByOpSign(operationLogRecordDTOs);
        return Response.builderSuccess(null);
    }
}
